package com.wasion.gydpe.ice;

import CommProxy.CBasicParameter;
import CommProxy._IWriteableDisp;
import Ice.Current;

public class CInputChannel extends _IWriteableDisp {
	protected String _name;
	protected CCustomReceiver _handler;
	protected CSinglePeer _singlePeer;

	public CInputChannel(String name, CCustomReceiver recvHandler) {
		_name = name;
		_handler = recvHandler;
		_singlePeer = null;
	}

	public void setReceiver(CCustomReceiver recvHandler) {
		_handler = recvHandler;
	}

	public void setSinglePeer(CSinglePeer singlePeer) {
		_singlePeer = singlePeer;
	}

	public int trans(CBasicParameter frame, Current current) {

		int ret = CCustomReceiver.CP_UNKNOWN_ERROR;

		if (_handler != null) {
			ret = _handler.recv(frame); // 实际接受
			if (frame.isResponse) // 是阻塞发送的回应报文
			{
				try {
					_singlePeer._mutex.lock();
					_singlePeer._revId = frame.seqID;
					_singlePeer._received_answer.signalAll();
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					_singlePeer._mutex.unlock();
				}
			}
		} else {
			ret = CCustomReceiver.CP_NO_HANDLER;
		}

		return ret;
	}
}